package com.iiiss.template.ssg.common;

import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import org.springframework.transaction.annotation.Transactional;

import com.iiiss.template.ssh.common.core.IBean;
import com.iiiss.template.ssh.common.core.IPersistenceManager;

/*
 * TemplateSSG - com.iiiss.template.ssg.common - AppenginePM.java
 * 
 * Copyright (C) 2011  Mingye Cheng <Cheng, Mingye at GMAIL>
 * 
 * AppenginePM.java is part of <iiiStudio Template Framework> project.
 * This project is hosted at <http://iiiss-template-ssh.googlecode.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Revision history:
 * [2011-5-30] Mingye Cheng - Initial version established.
 * [2011-5-31] Mingye Cheng - Modified for GoogleAppEngine. (Using JpaDaoSupport)
 * [2011-7-6] Mingye Cheng - 'getImplClass()' modified
 *                           ApplicationContextAware added,
 *                           moved to 'template.ssg' package.
 */
@Transactional
public class GaeJpaPM extends JpaDaoSupport implements IPersistenceManager,
		ApplicationContextAware {
	@Autowired
	private ApplicationContext context;

	public void setApplicationContext(ApplicationContext context)
			throws BeansException {
		this.context = context;
	}

	@SuppressWarnings("unchecked")
	private Class<IBean> getImplClass(String beanName) {
		return (Class<IBean>) context.getBean(beanName).getClass();
	}

	public IBean newObject(String beanName) {
		// scope="prototype"
		IBean bean = (IBean) context.getBean(beanName);
		getJpaTemplate().persist(bean);
		return bean;
	}

	public <T extends IBean> T getObject(String beanName, long id) {
		List<T> objectList = getObjects(beanName, "where key = " + id);
		return objectList.isEmpty() ? null : objectList.get(0);
	}

	public <T extends IBean> T getObject(String beanName, String query) {
		List<T> objectList = getObjects(beanName, query);
		return objectList.isEmpty() ? null : objectList.get(0);
	}

	@SuppressWarnings("unchecked")
	public <T extends IBean> List<T> getObjects(String beanName, String query) {
		query = query == null ? "" : query;
		return getJpaTemplate().find(
				"SELECT o from " + getImplClass(beanName).getName() + " o "
						+ query);
	}

	public void deteleObject(IBean object) {
		getJpaTemplate().remove(object);
	}
}
